package service

import (
	"fmt"
	"log"
	"math/rand"
	"mygin_project/dao"
	"mygin_project/model"
	"mygin_project/tool"
	"time"
)

type SmsService struct {
	
}

func (server *SmsService)SendSmsCode(phone string) int  {
	//生成验证码
	code := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))

	if code == "" {
		return tool.ERROR
	}
	//返回数据
	log.Printf("send code is %s", code)

	var smsCode = model.SmsCode{
		Phone: phone,
		Code: code,
		Status: 1,
		CreateTime: time.Now().Unix(),
		//ModifyTime: time.Now(),
	}
	smsDao := dao.SmsDao{Orm: tool.DbEngine}
	result := smsDao.InsertData(smsCode)
	log.Printf("save code is %v", result)

	if result {
		return tool.SUCCESS
	}

	return tool.ERROR
}
